package org.apache.rocketmq.logging.inner.appenders;

import org.apache.rocketmq.logging.inner.SysLogger;


public class ConsoleAppender extends WriterAppender {

    public static final String SYSTEM_OUT = "System.out";
    public static final String SYSTEM_ERR = "System.err";

    protected String target = SYSTEM_OUT;

    public ConsoleAppender() {
    }

    public void setTarget(String value) {
        String v = value.trim();

        if (SYSTEM_OUT.equalsIgnoreCase(v)) {
            target = SYSTEM_OUT;
        } else if (SYSTEM_ERR.equalsIgnoreCase(v)) {
            target = SYSTEM_ERR;
        } else {
            targetWarn(value);
        }
    }

    public String getTarget() {
        return target;
    }

    void targetWarn(String val) {
        SysLogger.warn("[" + val + "] should be System.out or System.err.");
        SysLogger.warn("Using previously set target, System.out by default.");
    }

    public void activateOptions() {
        if (target.equals(SYSTEM_ERR)) {
            setWriter(createWriter(System.err));
        } else {
            setWriter(createWriter(System.out));
        }
        super.activateOptions();
    }

    protected final void closeWriter() {

    }
}